package com.wenhao.leetcode.medium;

import com.wenhao.leetcode.structure.ListNode;

import java.sql.ResultSet;

/**
 * 题目：https://leetcode-cn.com/problems/linked-list-cycle-ii/
 * 环形链表
 *
 * @author Wenhao Tong
 * @create 2021-07-17 19:37
 */
public class LeetCode142 {
    public static void main(String[] args) {

    }
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast.next == null) {
            return null;
        }
        while (head != slow) {
            head = head.next;
            slow = slow.next;
        }
        return head;
    }
}
